﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmCapNhatGia : Form
    {
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private HangHoaManagement hanghoaMgt = new HangHoaManagement();
        private DonGiaNguyenVatLieuManagement dongianguyenvatlieuMgt = new DonGiaNguyenVatLieuManagement();
        private CTBangKeNhapHangMgt ctbangkenhaphangMgt = new CTBangKeNhapHangMgt();

        private BangKeNhapHang bangkenhaphang;
        public BangKeNhapHang BangKeNhapHang
        {
            set { this.bangkenhaphang = value; }
            get { return this.bangkenhaphang; }
        }

        public FrmCapNhatGia()
        {
            InitializeComponent();

            cbNhaCungCap.DataSource = GetAllNhaCungCap();
            cbNhaCungCap.DisplayMember = "TenNhaCungCap";
            cbNhaCungCap.ValueMember = "Id";

            cbVL.DataSource = GetAllHangHoa();
            cbVL.DisplayMember = "Ten";
            cbVL.ValueMember = "Id";
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (txtDonGia.Text == "")
            {
                MessageBox.Show("Bạn chưa nhập đơn giá");
            }
            else
            {
                int count = dgvVatLieu.Rows.Count;
                ResultObj result = new ResultObj();
                lblID.Text = result.ReturnCode.ToString();
                for (int i = 0; i < count; i++)
                {
                    NhaCungCap nhacungcap = (NhaCungCap)cbNhaCungCap.SelectedItem;
                    DonGiaNguyenVatLieu dongianguyenvatlieu = new DonGiaNguyenVatLieu();
                    dongianguyenvatlieu.IdHangHoa = ((HangHoa)cbVL.SelectedItem).Id;
                    dongianguyenvatlieu.Id = Convert.ToInt32(lblID.Text);
                    dongianguyenvatlieu.IdNhaCungCap = nhacungcap.Id;
                    dongianguyenvatlieu.NgayThang = dtpNgayCapNhat.Value;
                    dongianguyenvatlieu.DonGia = Convert.ToInt64(txtDonGia.Text.Replace(".",""));
                    dongianguyenvatlieuMgt.UpdateDonGiaNguyenVatLieu(dongianguyenvatlieu);

                    List<CTBangKeNhapHang> lstCTBangkenhaphang = GetAllCTBangKeNhapHang();
                    List<HangHoa> lstHangHoa = GetAllHangHoa();
                    for (int j = 0; j < lstHangHoa.Count; j++)
                    {
                        if (lstHangHoa[j].Id == dongianguyenvatlieu.IdHangHoa)
                        {
                            for (int k = 0; k < lstCTBangkenhaphang.Count; k++)
                            {
                                if (lstHangHoa[j].MaSo == lstCTBangkenhaphang[k].MaVatLieu)
                                {
                                    lstCTBangkenhaphang[k].DonGia = dongianguyenvatlieu.DonGia;
                                    ctbangkenhaphangMgt.UpdateCTBangKeNhapHang(lstCTBangkenhaphang[k]);
                                }
                            }
                        }
                    }
                }

                MessageBox.Show("Lưu thành công.", "Thông báo");
                this.Close();
            }
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj resultObj = new ResultObj();
            List<NhaCungCap> lstNhaCungCap = new List<NhaCungCap>();

            resultObj = nhacungcapMgt.GetAllNhaCungCap();
            lstNhaCungCap = (List<NhaCungCap>)resultObj.Result;

            return lstNhaCungCap;
        }

        public List<CTBangKeNhapHang> GetAllCTBangKeNhapHang()
        {
            ResultObj resultObj = new ResultObj();
            List<CTBangKeNhapHang> lstCTBangKeNhapHang = new List<CTBangKeNhapHang>();

            resultObj = ctbangkenhaphangMgt.GetAllCTBangKeNhapHang();
            lstCTBangKeNhapHang = (List<CTBangKeNhapHang>)resultObj.Result;

            return lstCTBangKeNhapHang;
        }

        public List<HangHoa> GetAllHangHoa()
        {
            ResultObj resultObj = new ResultObj();
            List<HangHoa> lstHangHoa = new List<HangHoa>();

            resultObj = hanghoaMgt.GetAllHangHoa();
            lstHangHoa = (List<HangHoa>)resultObj.Result;

            return lstHangHoa;
        }

        public List<DonGiaNguyenVatLieu> GetAllDonGiaNguyenVatLieu()
        {
            ResultObj resultObj = new ResultObj();
            List<DonGiaNguyenVatLieu> lstDonGiaNguyenVatLieu = new List<DonGiaNguyenVatLieu>();

            resultObj = dongianguyenvatlieuMgt.GetAllDonGiaNguyenVatLieu();
            lstDonGiaNguyenVatLieu = (List<DonGiaNguyenVatLieu>)resultObj.Result;

            return lstDonGiaNguyenVatLieu;
        }

        private void cbNhaCungCap_SelectedValueChanged(object sender, EventArgs e)
        {
            ResultObj resultObj = new ResultObj();

            NhaCungCap nhacungcap = (NhaCungCap)cbNhaCungCap.SelectedItem;
            DonGiaNguyenVatLieu dongianguyenvatlieu = new DonGiaNguyenVatLieu();
            dongianguyenvatlieu.Id = -1;
            dongianguyenvatlieu.IdHangHoa = -1;
            dongianguyenvatlieu.IdNhaCungCap = nhacungcap.Id;

            resultObj = dongianguyenvatlieuMgt.GetDonGiaNguyenVatLieu(dongianguyenvatlieu);
            List<DonGiaNguyenVatLieu> lst = (List<DonGiaNguyenVatLieu>)resultObj.Result;
   
            dgvVatLieu.AutoGenerateColumns = false;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtDonGia.Text == "")
            {
                MessageBox.Show("Vui lòng nhập đơn giá !");
                return;
            }
            ResultObj resultObj = new ResultObj();

            HangHoa hanghoa = (HangHoa)cbVL.SelectedItem;
            NhaCungCap nhacungcap = (NhaCungCap)cbNhaCungCap.SelectedItem;
            DonGiaNguyenVatLieu dongianguyenvatlieu = new DonGiaNguyenVatLieu();

            dongianguyenvatlieu.IdNhaCungCap = nhacungcap.Id;
            dongianguyenvatlieu.IdHangHoa = hanghoa.Id;
            dongianguyenvatlieu.DonGia = Convert.ToInt64(txtDonGia.Text);
            dongianguyenvatlieu.NgayThang = dtpNgayCapNhat.Value;

            lblID.Text = (dongianguyenvatlieuMgt.SaveNewDonGiaNguyenVatLieu(dongianguyenvatlieu)).ReturnCode.ToString();
            MessageBox.Show("Thêm thành công.","Thông báo");
            
            dongianguyenvatlieu = new DonGiaNguyenVatLieu();
            dongianguyenvatlieu.Id = -1;
            dongianguyenvatlieu.IdHangHoa = -1;
            dongianguyenvatlieu.IdNhaCungCap = nhacungcap.Id;

            resultObj = dongianguyenvatlieuMgt.GetDonGiaNguyenVatLieu(dongianguyenvatlieu);
            List<DonGiaNguyenVatLieu> lst = (List<DonGiaNguyenVatLieu>)resultObj.Result;
           
            dgvVatLieu.Rows.Clear();
            dgvVatLieu.AutoGenerateColumns = false;
            for (int i = 0; i < lst.Count; i++)
            {
                dgvVatLieu.Rows[i].Cells[0].Value = lst[i].Id;
                dgvVatLieu.Rows[i].Cells[2].Value = lst[i].DonGia;
                dgvVatLieu.Rows[i].Cells[1].Value = lst[i].hanghoa.Ten;
            }          
        }

        private void txtDonGia_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                MessageBox.Show("Vui lòng nhập số !");
                e.Handled = true;
            }
        }

        private void txtDonGia_TextChanged(object sender, EventArgs e)
        {
            if (txtDonGia.Text != "")
            {
                txtDonGia.SelectionStart = txtDonGia.Text.Length + 1;
                string num = txtDonGia.Text.Replace(".", "");
                txtDonGia.Text = (Convert.ToInt64(num).ToString("N0")).Replace(",",".");
            }
        }
    }
}
